/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package enterprise.web_jpa_war.servlet;

import enterprise.web_jpa_war.entity.Cusorder;
import enterprise.web_jpa_war.entity.Customer;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import javax.persistence.PersistenceUnit;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityManager;
import javax.annotation.Resource;
import javax.transaction.UserTransaction;

/**
 *
 * @author GOSTEP
 */
@WebServlet(name="CreateOrderServlet", urlPatterns={"/CreateOrder.do"})
public class CreateOrderServlet extends HttpServlet {
    @PersistenceUnit
    //The emf corresponding to
    private EntityManagerFactory emf;

    @Resource
    private UserTransaction utx;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet CreateOrderServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet CreateOrderServlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    } 

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //processRequest(request, response);
    } 


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        assert emf != null;             //Make sure injection went through correctly.
        EntityManager em = null;
        try {
             //Get the data from user's form
            String orderitem = request.getParameter("orderitem");
            String dateoforder  = request.getParameter("dateoforder");
            String duedate  = request.getParameter("duedate");
            String company  = request.getParameter("company");
            String customerid = request.getParameter("customerid");
            utx.begin();
            //create an em.
            //Since the em is created inside a transaction, it is associsated with
            //the transaction
            em = emf.createEntityManager();
             
            Customer or= em.find(Customer.class, customerid);
            if(or==null){
             request.setAttribute("message","Your Customer Id not in the database. find the correct customer id");
             request.getRequestDispatcher("message.jsp").forward(request, response);
            }else{
            //Create a Order instance out of it
         

            Cusorder order= new Cusorder(orderitem, dateoforder, duedate, company, or);
            //persist the person entity
            
            em.persist(order);
            //commit transaction which will trigger the em to
            //commit newly created entity into database
            
            utx.commit();

             request.setAttribute("message","You have created a new Order...");
             request.getRequestDispatcher("message.jsp").forward(request, response);
          }
        } catch (Exception ex) {
            throw new ServletException(ex);
        } finally {
            //close the em to release any resources held up by the persistebce provider
            if(em != null) {
                em.close();
            }
        }

    }


    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
